<template>
  <div class="water-flow-adjustment-flow">
    <h4>水流量调节流程</h4>
    <div class="main-container">
      <!-- 左侧流程步骤区域 -->
      <div class="flow-container">
        <!-- 步骤一 -->
        <div class="flow-step step-one" @click="showStepOneDialog = true">
          <div class="step-number">1</div>
          <div class="step-content">
            <h5>检查水流量情况</h5>
            <p>查看机组控制屏的发电机面板</p>
          </div>
        </div>

        <!-- 流程箭头 -->
        <div class="flow-arrow">
          <svg width="40" height="20" viewBox="0 0 40 20">
            <line
              x1="0"
              y1="10"
              x2="30"
              y2="10"
              stroke="#3498db"
              stroke-width="2"
            />
            <polygon points="30,5 40,10 30,15" fill="#3498db" />
          </svg>
        </div>

        <!-- 步骤二 -->
        <div
          class="flow-step step-two"
          :class="{
            completed: waterFlowAdjustmentStore.getStepTwoStatus,
            unlocked: waterFlowAdjustmentStore.getStepOneStatus,
          }"
        >
          <div class="step-number">2</div>
          <div class="step-content">
            <h5>调整水流量</h5>
            <p>根据实际情况调节水流量</p>
          </div>
          <button
            class="adjust-flow-button"
            :disabled="!waterFlowAdjustmentStore.getStepOneStatus"
            @click="showAdjustFlowDialog = true"
          >
            调节水流量
          </button>
        </div>

        <!-- 流程箭头 -->
        <div class="flow-arrow">
          <svg width="40" height="20" viewBox="0 0 40 20">
            <line
              x1="0"
              y1="10"
              x2="30"
              y2="10"
              stroke="#3498db"
              stroke-width="2"
            />
            <polygon points="30,5 40,10 30,15" fill="#3498db" />
          </svg>
        </div>

        <!-- 步骤三 -->
        <div
          class="flow-step step-three"
          :class="{
            completed: waterFlowAdjustmentStore.getStepThreeStatus,
            unlocked: waterFlowAdjustmentStore.getStepTwoStatus,
          }"
        >
          <div class="step-number">3</div>
          <div class="step-content">
            <h5>检查趋势曲线</h5>
            <p>查看4组发电机定子绕组温度变量</p>
          </div>
          <button
            class="view-trend-button"
            :disabled="!waterFlowAdjustmentStore.getStepTwoStatus"
            @click="openTrendChart"
          >
            查看趋势曲线
          </button>
        </div>
      </div>
    </div>

    <!-- 步骤一的提示对话框 -->
    <div
      v-if="showStepOneDialog"
      class="dialog-overlay"
      @click="closeStepOneDialog"
    >
      <div class="dialog-content" @click.stop>
        <h5>检查机组控制屏的发电机面板</h5>
        <p>请在发电机面板上查看水流量情况，确认当前的水流量状态。</p>
        <div class="dialog-buttons">
          <button class="cancel-button" @click="closeStepOneDialog">
            取消
          </button>
          <button class="confirm-button" @click="openGeneratorPanel">
            打开发电机面板
          </button>
        </div>
      </div>
    </div>

    <!-- 步骤二的水流量调节对话框 -->
    <div
      v-if="showAdjustFlowDialog"
      class="dialog-overlay"
      @click="closeAdjustFlowDialog"
    >
      <div class="dialog-content" @click.stop>
        <h5>水流量调节</h5>
        <!-- <p class="status-info" v-if="isLowFlow">当前检测到水流量偏低</p> -->
        <div class="flow-control-section">
          <div class="flow-display">
            <span class="flow-label">当前水流量:</span>
            <span class="flow-value">{{ waterFlow }}</span>
            <span class="flow-unit">L/min</span>
          </div>

          <div class="slider-container">
            <el-slider
              v-model="waterFlow"
              :min="20"
              :max="80"
              :step="1"
              :show-tooltip="true"
              tooltip-position="top"
              class="water-flow-slider"
            />
            <div class="slider-labels">
              <span>0</span>
              <span class="warning-label">50</span>
              <span>100</span>
            </div>
          </div>
        </div>
        <div class="dialog-buttons">
          <button class="cancel-button" @click="closeAdjustFlowDialog">
            取消
          </button>
          <button
            class="confirm-button adjust-button"
            :disabled="waterFlowAdjustmentStore.getAdjustingStatus"
            @click="adjustWaterFlow"
          >
            {{
              waterFlowAdjustmentStore.getAdjustingStatus
                ? "调节中..."
                : "确认调节"
            }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from "vue";
import { ElMessage, ElSlider } from "element-plus";
import { setAlgorithmParam } from "@/api/util";
import { useUserStore } from "@/store/user";
import { useWaterFlowAdjustmentStore } from "@/store/waterFlowAdjustment";

// 定义组件props
const props = defineProps({
  initialFlow: {
    type: Number,
    default: 50,
  },
});

// 获取用户store实例
const userStore = useUserStore();
// 获取水流量调节store实例
const waterFlowAdjustmentStore = useWaterFlowAdjustmentStore();

// 定义组件事件
const emit = defineEmits([
  "open-generator-panel",
  "flow-adjusted",
  "open-trend-chart",
]);

// 对话框状态管理
const showStepOneDialog = ref(false);
const showAdjustFlowDialog = ref(false);

// 水流量状态
const waterFlow = ref(props.initialFlow);

// 计算属性：判断是否为低流量
const isLowFlow = computed(() => waterFlow.value < 50);

// 关闭步骤一对话框
const closeStepOneDialog = () => {
  showStepOneDialog.value = false;
};

// 关闭步骤二对话框
const closeAdjustFlowDialog = () => {
  showAdjustFlowDialog.value = false;
};

// 打开发电机面板
const openGeneratorPanel = () => {
  // 使用store的action设置步骤一为已完成
  waterFlowAdjustmentStore.completeStepOne();
  showStepOneDialog.value = false;
  // 触发事件通知父组件打开发电机面板
  emit("open-generator-panel");
};

// 调节水流量函数
const adjustWaterFlow = async () => {
  try {
    // 使用store设置调节状态
    waterFlowAdjustmentStore.setAdjustingStatus(true);
    // 从user store获取copyNum
    const copyNum = userStore.copyNum;
    // 调用setAlgorithmParam设置水流量参数
    await setAlgorithmParam("2_41_0_0", waterFlow.value, copyNum);

    // 显示成功提示
    ElMessage.success(`水流量已成功调节至 ${waterFlow.value} L/min`);

    // 关闭对话框
    showAdjustFlowDialog.value = false;

    // 使用store标记步骤二为已完成
    waterFlowAdjustmentStore.completeStepTwo();

    // 触发水流量已调节事件
    emit("flow-adjusted", waterFlow.value);
  } catch (error) {
    console.error("调节水流量失败:", error);
    ElMessage.error("水流量调节失败，请稍后重试");
  } finally {
    // 无论成功失败，都设置调节状态为false
    waterFlowAdjustmentStore.setAdjustingStatus(false);
  }
};

// 打开趋势图表
const openTrendChart = () => {
  // 使用store标记步骤三为已完成
  waterFlowAdjustmentStore.completeStepThree();
  // 触发事件通知父组件打开趋势图表
  emit("open-trend-chart");
};
</script>

<style scoped>
.water-flow-adjustment-flow {
  background: #2c3e50;
  border-radius: 8px;
  padding: 24px;
  color: #ecf0f1;
  min-width: 300px;
}

.water-flow-adjustment-flow h4 {
  color: #3498db;
  margin-top: 0;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #34495e;
  font-size: 18px;
  font-weight: 600;
}

.main-container {
  display: flex;
  gap: 20px;
  min-height: 400px;
}

/* 左侧流程步骤区域 */
.flow-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* 流程步骤样式 */
.flow-step {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background: #34495e;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 4px solid #7f8c8d;
}

.flow-step:hover {
  background: #3c566a;
  transform: translateX(5px);
}

.flow-step.unlocked {
  border-left-color: #3498db;
  cursor: pointer;
}

.flow-step.completed {
  border-left-color: #27ae60;
  background: #2d3e50;
}

.flow-step.step-two.completed {
  border-left-color: #27ae60;
}

.step-number {
  width: 32px;
  height: 32px;
  background: #7f8c8d;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 16px;
}

.flow-step.unlocked .step-number {
  background: #3498db;
}

.flow-step.completed .step-number {
  background: #27ae60;
}

.step-content {
  flex: 1;
}

.step-content h5 {
  margin: 0 0 8px 0;
  color: #ecf0f1;
  font-size: 16px;
  font-weight: 600;
}

.step-content p {
  margin: 0;
  color: #95a5a6;
  font-size: 14px;
}

/* 流程箭头 */
.flow-arrow {
  display: flex;
  justify-content: center;
  margin: -8px 0;
}

/* 按钮样式 */
.adjust-flow-button,
.view-trend-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  font-size: 14px;
}

.adjust-flow-button:hover:not(:disabled),
.view-trend-button:hover:not(:disabled) {
  background: #2980b9;
}

.adjust-flow-button:disabled,
.view-trend-button:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}

/* 对话框样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-content {
  background: #2c3e50;
  border-radius: 8px;
  padding: 24px;
  min-width: 400px;
  max-width: 600px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.dialog-content h5 {
  margin: 0 0 16px 0;
  color: #3498db;
  font-size: 18px;
  font-weight: 600;
}

.status-info {
  background-color: #e74c3c;
  color: white;
  padding: 12px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-weight: 500;
  font-size: 14px;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.cancel-button {
  background: #7f8c8d;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.cancel-button:hover {
  background: #95a5a6;
}

.confirm-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.confirm-button:hover:not(:disabled) {
  background: #2980b9;
}

.confirm-button:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}

/* 水流量调节对话框内部样式 */
.flow-control-section {
  margin-bottom: 16px;
}

.flow-display {
  display: flex;
  align-items: baseline;
  justify-content: center;
  margin-bottom: 20px;
  padding: 16px;
  background-color: #34495e;
  border-radius: 8px;
}

.flow-label {
  font-size: 16px;
  color: #95a5a6;
  margin-right: 8px;
}

.flow-value {
  font-size: 28px;
  font-weight: bold;
  color: #3498db;
  margin-right: 4px;
}

.flow-unit {
  font-size: 14px;
  color: #95a5a6;
}

.slider-container {
  margin-bottom: 16px;
}

.water-flow-slider {
  margin: 16px 0;
}

.slider-labels {
  display: flex;
  justify-content: space-between;
  color: #95a5a6;
  font-size: 12px;
  margin-top: 8px;
}

.warning-label {
  color: #e74c3c;
  font-weight: bold;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .main-container {
    flex-direction: column;
  }

  .dialog-content {
    min-width: 280px;
    margin: 20px;
  }

  .flow-step {
    flex-direction: column;
    align-items: flex-start;
  }

  .dialog-buttons {
    flex-direction: column;
  }

  .cancel-button,
  .confirm-button {
    width: 100%;
  }
}
</style>